package com.sg.plugin.obff.interceptor;

import com.alibaba.fastjson.JSON;
import com.wicket.okrobff.common.baseinfo.AuthContext;
import com.wicket.okrobff.common.baseinfo.BaseInfoDO;
import com.wicket.okrobff.common.baseinfo.BaseInfoHolder;
import com.sg.common.ro.OkrResult;
import com.sg.common.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;


/**
 * @author ：CHS
 * @version 1.0
 * @since 2022/3/29 16:14
 */
@Component
@Slf4j
public class CommonInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {


        String loginIp = "127.0.0.1";
        String referer = request.getHeader("Referer");
        if (referer != null && !referer.isEmpty()) {
            java.net.URL url = new java.net.URL(referer);
            String host = url.getHost();
            loginIp = host;
        }

        String source = "PC";
        String authSource = request.getHeader("X-Auth-Souce");
        if (authSource != null && !authSource.isEmpty()) {
            source = authSource;
        }

        if (BaseInfoHolder.contextHolder.get() != null) {
            AuthContext authContext = BaseInfoHolder.contextHolder.get();
            BaseInfoDO baseInfoDO = authContext.getBaseInfo();
            baseInfoDO.setSpecLoginIp(loginIp);
            baseInfoDO.setSpecSource(source);
        } else {
            BaseInfoDO baseInfoDO = new BaseInfoDO();
            baseInfoDO.setSpecLoginIp(loginIp);
            baseInfoDO.setSpecSource(source);
            AuthContext authContext = new AuthContext();
            authContext.setBaseInfo(baseInfoDO);
            BaseInfoHolder.contextHolder.set(authContext);
        }

        return true;


    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception e) throws Exception {

        BaseInfoHolder.contextHolder.remove();
    }

    private void writeError2Response(HttpServletResponse response, String message) {
        try {
            OkrResult<String> notAuthorityResult = OkrResult.fail("0", message);
            response.setCharacterEncoding(StandardCharsets.UTF_8.name());
            response.setContentType(MediaType.APPLICATION_JSON_VALUE);
            PrintWriter writer = response.getWriter();
            writer.write(JSON.toJSONString(notAuthorityResult));
            writer.close();
        } catch (IOException e) {
            log.error("响应消息写入失败.", e);
        }
    }

}

